/*
 * Copyright (C) <2009>  <Matthew Dickinson>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.pulsarwebdevelopment.concordance.plugins;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

//import javax.swing.JOptionPane;

/**
 * Processes text file and leaves what to do with individual words to subclasses
 * @author Matthew Dickinson
 * Last updated: 11/9/2008
 */
public abstract class UniqueX implements Plugin{

	public File readFile;
	public File exportFile;

	/**
	 * Contains generic code for reading through a file (char by char) 
	 * and outputting the text to another file after performing per-program checks 
	 */
	public void parseFile(){
		try{
			//open files
			BufferedReader inFile = new BufferedReader(new FileReader(readFile));
			BufferedWriter outFile = new BufferedWriter(new FileWriter(exportFile));

			String line;	//current line
			String word;	//current word
			StringBuffer buildWord = new StringBuffer();	//word in the process of being built
			char[] nextLine;	//array representation of line

			outFile.write(getHeader());
			while((line = inFile.readLine()) != null){
				outFile.write(getParagrapStart());	
				nextLine = line.toCharArray();

				for(int i = 0; i < nextLine.length; i++){
					char curChar = nextLine[i];

					//if this character is a letter add it to the buffer and move on
					if(Character.isLetter(curChar)){
						buildWord.append(curChar);
						continue;
					}

					//if this character is a single quote that is not at the beginning of the word,
					//it must be an apostrophe. add it to the buffer and move on
					if(curChar == '\'' && buildWord.length() > 0){
						buildWord.append(curChar);
						continue;
					}

					//if this character is a dash, the next character is a letter and there is already stuff in the buffer,
					//this character must be a hyphen. add it to the buffer and move on
					if(curChar == '-' && buildWord.length() > 0 && Character.isLetter(nextLine[i+1])){
						buildWord.append(curChar);
						continue;
					}

					//if this character didn't match any of the other cases, it must be a delimiter
					//check the status. if there is anything in it, format it and print it out
					if(buildWord.length() > 0){
						word = buildWord.toString();
						buildWord = new StringBuffer();
						outFile.write(handleWord(word));
					}

					//write delimiting character
					outFile.write(curChar);

				}

				//line was ended improperly. treat it as if there had been punctuation
				if(buildWord.length() > 0){
					word = buildWord.toString();
					buildWord = new StringBuffer();
					outFile.write(handleWord(word));
					System.out.println("Line ended without punctuation: " + word);
				}
				//insert line break
				outFile.write(getParagrapEnd());
			}
			outFile.write(getFooter());
			outFile.close();
			inFile.close();

		}catch(Exception e){
			String message = e.getMessage();
			if(message ==  null){
				message = "An unhandled error occured";
			}
			//TODO display error
			//JOptionPane.showMessageDialog(null, message);
			e.printStackTrace();
			System.exit(1);
		}
	}

	/**
	 * Gets the appropriate type of header
	 * @return The string that should start this file
	 */
	public abstract String getHeader();
	
	/**
	 * Gets the appropriate type of footer
	 * @return The String that should end this file
	 */
	public abstract String getFooter();
	
	/**
	 * Gets the appropriate paragraph start 
	 * @return The String that should start paragraphs
	 */
	public abstract String getParagrapStart();
	
	/**
	 * Gets the appropriate paragraph end 
	 * @return The String that should end paragraphs
	 */
	public abstract String getParagrapEnd();

	/**
	 * formats String according to the rules of this program (highlight if unique, &c.)
	 * @param word String to format
	 * @return Formatted String to print out
	 */
	public abstract String handleWord(String word);

}
